﻿Imports Mystery.DbConnection
Imports System.Data.SqlClient

''' <summary>
''' a mapper which help in converting content in db formats
''' </summary>
''' <remarks></remarks>
<GlobalAvalibleObjectImplementation(singleton:=True)>
Public Class DatabaseContentHelper

    Public Property ContentPropertyDBType As IDictionary(Of Type, String)

    Public Sub New()

        ContentPropertyDBType = New Dictionary(Of Type, String)
        ContentPropertyDBType(GetType(String)) = "nvarchar(MAX)"
        ContentPropertyDBType(GetType(Double)) = "float"
        ContentPropertyDBType(GetType(Integer)) = "bigint"
        ContentPropertyDBType(GetType(Date)) = "datetime"
        ContentPropertyDBType(GetType(Boolean)) = "bit"

    End Sub


    Public Sub FixesOnCreation(property_type As Type, table_name As String, column_name As String)
        Dim conn As IConnection = getGlobalObject(Of IConnection)()

        Select Case property_type
            Case GetType(String)
                conn.executeNonQuery("update [" & table_name & "] set [" & column_name & "]=@default", New SqlParameter("default", ""))
            Case GetType(Boolean)
                conn.executeNonQuery("update [" & table_name & "] set [" & column_name & "]=@default", New SqlParameter("default", 0))
        End Select
    End Sub

End Class
